package com.example.rpc_core.loadbalance.impl;

import com.example.rpc_core.loadbalance.LoadBalancer;
import com.example.rpc_core.model.ServiceInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 随机负载均衡器
 */
public class RandomLoadBalancer implements LoadBalancer {

    private final Random random= new Random();
    @Override
    public ServiceInfo select(Map<String, Object> requestParams, List<ServiceInfo> serviceInfoList) {
        int size = serviceInfoList.size();
        if (size==0){
            return null;
        }
        if(size==1){
            return serviceInfoList.get(0);
        }
        int i = random.nextInt(size);
        return serviceInfoList.get(i);
    }
}
